package jok.io;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileDemo {

	public static void main(String[] args) throws IOException {
		{
			// 构造器
			File file1 = new File("D:\\Test\\AA\\bb.txt");
			File file2 = new File("D:/Test/AA");
			File file3 = new File("D:\\Test\\AA", "bb.txt");
			File file4 = new File(file2, "bb.txt");
			File file5 = new File("Test\\AA\\bb.txt");// 相对路径：相对于项目根目录
		}
		{
			File file = new File("D:\\test.txt");
			// 创建文件
			boolean success = file.createNewFile();
			System.out.println(success);
			// 删除文件
			success = file.delete();
			System.out.println(success);
		}
		System.out.println("------------------------------");
		{
			File file = new File("D:\\Test");
			// 创建文件夹
			boolean success = file.mkdir();
			System.out.println(success);
			// 删除文件夹
			success = file.delete();
			System.out.println(success);
		}
		System.out.println("------------------------------");
		{
			File file = new File("D:\\Test\\AA");
			// 创建多层文件夹
			boolean success = file.mkdirs();
			System.out.println(success);
			// 不能删除非空文件夹，因此不能同时删除多层文件夹，需逐层删除
			success = file.delete();
			System.out.println(success);
			file = new File("D:\\Test");
			success = file.delete();
			System.out.println(success);
		}
		System.out.println("------------------------------");
		{
			File file = new File("D:\\Test\\AA");
			file.mkdirs();
			file = new File("D:\\Test\\AA\\aa.txt");
			file.createNewFile();
			System.out.println(file.exists());
			System.out.println(file.isFile());
			System.out.println(file.isDirectory());
			System.out.println(file.isAbsolute());
			System.out.println(file.canRead());
			System.out.println(file.canWrite());
			System.out.println(file.isHidden());

			deleteFile("D:\\Test");
		}
		System.out.println("------------------------------");
		{
			File file = new File("D:\\Test\\AA");
			file.mkdirs();
			file = new File("D:\\Test\\AA\\aa.txt");
			file.createNewFile();
			System.out.println(file.getAbsolutePath());
			// D:\Program Files (x86)\eclipse\workspace\java-se-demo\Test\AA\bb.txt
			System.out.println(file.getPath());// Test\AA\bb.txt
			System.out.println(file.getName());// bb.txt
			System.out.println(file.length());// 0
			System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(new Date(file.lastModified())));// 2018-06-08 14:36:55

			deleteFile("D:\\Test");
		}
		System.out.println("------------------------------");
		{
			File file = new File("D:\\Test\\AA");
			file.mkdirs();
			file = new File("D:\\Test\\BB");
			file.mkdirs();
			file = new File("D:\\Test\\AA\\aa.txt");
			file.createNewFile();
			file = new File("D:\\Test\\BB\\bb.txt");
			file.createNewFile();
			file = new File("D:\\Test\\cc.txt");
			file.createNewFile();

			File[] files = File.listRoots();
			for (File f : files) {
				System.out.println(f.getPath());
			}

			file = new File("D:\\Test");
			files = file.listFiles();
			for (File f2 : files) {
				System.out.println(f2.getAbsolutePath());
			}

			String[] names = file.list();
			for (String name : names) {
				System.out.println(name);
			}

			deleteFile("D:\\Test");
		}
		System.out.println("------------------------------");
		{
			File file = new File("D:\\Test\\AA");
			file.mkdirs();
			file = new File("D:\\Test\\BB");
			file.mkdirs();
			file = new File("D:\\Test\\AA\\aa.txt");
			file.createNewFile();
			file = new File("D:\\Test\\BB\\bb.txt");
			file.createNewFile();
			file = new File("D:\\Test\\cc.txt");
			file.createNewFile();

			file = new File("D:\\Test");
			listAllFiles(file);

			deleteAllFilesWithoutDir(file);

			deleteAllFiles(file);
			file.delete();
		}
	}

	private static int level = -1;

	/**
	 * 列出文件夹下的所有子文件夹和文件
	 * 
	 * @param file
	 */
	public static void listAllFiles(File file) {
		if (!file.exists() || !file.isDirectory() || file.isFile()) {
			return;
		}

		File[] files = file.listFiles();

		level++;

		String tab = "";
		for (int i = 0; i < level; i++) {
			tab += "\t";
		}

		for (File f : files) {
			System.out.println(tab + f.getName());
			if (f.isDirectory()) {
				listAllFiles(f);
			}
		}

		level--;
	}

	/**
	 * 删除文件夹下的所有文件（不含文件夹）
	 * 
	 * @param file
	 */
	public static void deleteAllFilesWithoutDir(File file) {
		if (!file.exists() || !file.isDirectory()) {
			return;
		}

		File[] files = file.listFiles();

		for (File f : files) {
			if (f.isDirectory()) {
				deleteAllFilesWithoutDir(f);
			} else {
				f.delete();
			}
		}
	}

	/**
	 * 删除文件夹下的所有文件
	 * 
	 * @param file
	 */
	public static void deleteAllFiles(File file) {
		if (!file.exists() || !file.isDirectory()) {
			return;
		}

		File[] files = file.listFiles();

		for (File f : files) {
			if (f.isFile()) {
				f.delete();
			}

			if (f.isDirectory()) {
				deleteAllFiles(f);
				f.delete();
			}
		}
	}
	
	/**
	 * 删除文件或文件夹（包含子文件）
	 * 
	 * @param fileName
	 */
	public static void deleteFile(String fileName) {
		File file = new File(fileName);
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				deleteAllFiles(file);
				file.delete();
			}
		}
	}
}
